<?php

// +------------------------------------------------
// | Name： 星空后台 应用模块跳转
// +-------------------------------------------------
// | Version: V5.0
// +-------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-------------------------------------------------
// | Time: 2021-04-06 15:11
// +-------------------------------------------------
declare(strict_types=1);

namespace app\common\traits;

use think\exception\HttpResponseException;
use think\Response;

/**
 * @xk 自定义封装跳转
 *
 * @var [type]
 */
trait Jump
{
    /**
     * @xk 成功跳转
     *
     * @method success
     *
     * @param string $msg    [提示信息]
     * @param string $url    [跳转链接]
     * @param string $data   [返回参数]
     * @param array  $header [header参数]
     * @param int    $wait   [跳转展示时间]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-06T15:41:07+080
     */
    protected function success($msg = '', $url = null, $data = '', array $header = [], $wait = 10)
    {
        if (is_null($url) && isset($_SERVER['HTTP_REFERER'])) {
            $url = $_SERVER['HTTP_REFERER'];
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
        }
        $result = [
            'code' => 200,  // 访问成功
            'msg' => $msg,
            'data' => $data,
            'url' => $url,
            'wait' => $wait,
        ];
        $type = $this->getResponseType();
        if ('html' == $type) {
            $response = View(app('config')->get('app.dispatch_success_tmpl'), $result);
        } elseif ('json' == $type) {
            $response = Json($result);
        }
        $response->header($header);
        throw new HttpResponseException($response);
    }

    /**
     * @xk 操作错误跳转链接
     *
     * @method  error
     * @Version [:version]
     * @Author  xkyinzhe
     * @Time    2020-09-09T16:06:53+080
     *
     * @param string $msg    [错误信息描述]
     * @param [type] $url    [跳转链接]
     * @param string $data   [返回数据]
     * @param array  $header [返回header信息]
     * @param int    $wait   [等待时间]
     *
     * @return [type] [description]
     */
    protected function error($msg = '', $url = null, $data = '', array $header = [], $wait = 10)
    {
        if (is_null($url)) {
            $url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
        }

        $type = $this->getResponseType();
        $result = [
            'code' => 404,
            'msg' => $msg,
            'data' => $data,
            'url' => $url,
            'wait' => $wait,
        ];
        if ('html' == $type) {
            $response = view(app('config')->get('app.dispatch_error_tmpl'), $result);
        } elseif ('json' == $type) {
            $response = json($result);
        }
        $response->header($header);
        throw new HttpResponseException($response);
    }

    /**
     * @xk 返回接口封装
     *
     * @method result
     * @param  integer                 $code                     [description]
     * @param  string                  $msg                      [description]
     * @param  integer                 $result_code              [description]
     * @param  string                  $result_msg               [description]
     * @param  array                   $data                     [description]
     * @param  integer                 $count                    [description]
     * @param  array                   $header                   [description]
     * @param  string                  $type                     [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-15T17:34:29+080
     */
    protected function result($code = 200, $msg = '', $result_code = 200, $result_msg = '', $data = [], $count = 0, array $header = [], $type = 'json')
    {
        $data = $data?$data:(object)$data;
        switch ($result_code) {
          case '1':
            $result_code = 200;
            break;
          case '0':
            $result_code = 404;
            break;
        }
        $result = [
            'code' => $code,
            'msg' => $msg,
            'result_code' => $result_code,
            'result_msg' => $result_msg,
            'time' => time(), // 接口请求时间戳
            'resultData' => $data,
            'result_count' => $count,
        ];
        // createLog('push/',json_encode($result,JSON_UNESCAPED_UNICODE));
        $type = $type ?: $this->getResponseType();
        $response = Response::create($result, $type)->header($header);

        throw new HttpResponseException($response);
    }

    /**
     * URL 重定向.
     *
     * @param string    $url    跳转的 URL 表达式
     * @param array|int $params 其它 URL 参数
     * @param int       $code   http code
     * @param array     $with   隐式传参
     *
     * @return void
     *
     * @throws HttpResponseException
     */
    protected function redirect($url = [], $params = [], $code = 302)
    {
        if (is_integer($params)) {
            $code = $params;
            $params = [];
        }

        $response = Response::create($url, 'redirect', $code);
        throw new HttpResponseException($response);
    }

    /**
     * 获取当前的 response 输出类型.
     *
     * @return string
     */
    protected function getResponseType()
    {
        return (request()->isJson() || request()->isAjax() || request()->isPost()) ? 'json' : 'html';
    }
}
